package com.wm.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.wm.eduService.entity.EduComment;
import com.wm.eduService.mapper.EduCommentMapper;
import com.wm.eduService.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author sshdg
 * @since 2020-10-12
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    /**
     * 分页查询某个课程的所有评论
     *
     * @param commentPage 分页对象
     * @param courseId    课程id
     * @return
     */
    @Override
    public Map<String, Object> pageComment(Page<EduComment> commentPage, String courseId) {
        LambdaQueryWrapper<EduComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduComment::getCourseId, courseId);
        this.page(commentPage, wrapper);

        List<EduComment> records = commentPage.getRecords();
        long current = commentPage.getCurrent();
        long pages = commentPage.getPages();
        long size = commentPage.getSize();
        long total = commentPage.getTotal();
        //下一页
        boolean hasNext = commentPage.hasNext();
        //上一页
        boolean hasPrevious = commentPage.hasPrevious();

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = Maps.newHashMap();
        map.put("records", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
